//
//  ISOpenDeatilViewController.m
//  iPhoneSafety
//
//  Created by dlon 2018/12/31.
//  Copyright © 2018年 com.li.ding. All rights reserved.
//

#import "ISOpenDeatilViewController.h"
#import "UIColor+NWHex.h"
#import "NWLazyProperty.h"
#import <Masonry/Masonry.h>
#import <BlocksKit/UIControl+BlocksKit.h>
#import "ISStopProtectViewController.h"
#import <libextobjc/EXTScope.h>
#import "ISPasswordViewController.h"


@interface ISOpenDeatilViewController ()<ISPasswordViewControllerDelegate>

@property (nonatomic, strong) UIButton *actionButton;

@end

@implementation ISOpenDeatilViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.bgImgView];
    
    [self.view addSubview:self.titleLabel];
    [self.view addSubview:self.desLabel];
    [self.view addSubview:self.desImgView];
    
    [self.view addSubview:self.actionButton];
    
    [self.bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.desImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.equalTo(self.desImgView.mas_width);
        make.centerY.equalTo(self.view);
    }];
    
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(85.f);
    }];
    
    [self.desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(21.f);
        make.left.right.equalTo(self.view).insets(UIEdgeInsetsMake(0, 78, 0, 78));
    }];

    [self.actionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(202, 58.f));
        make.bottom.equalTo(self.view).offset(-51.f);
        make.centerX.equalTo(self.view);
    }];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    @weakify(self)
    [self.actionButton bk_addEventHandler:^(id sender) {
        @strongify(self)
        NSString *pwdStr = [ISPassword pwd];
        if (pwdStr.length == 0) {
            ISPasswordViewController *pwd = [ISPasswordViewController new];
            pwd.type = ISPasswordViewControllerTypeNew;
            pwd.delegate = self;
            [self.navigationController pushViewController:pwd animated:YES];
            return;
        }
        
        [[ISSafetyAlarmMamanger sharedInstance] startMonitoringWithStrategy:self.strategy];
        ISStopProtectViewController *vc = [ISStopProtectViewController new];
        [self.navigationController pushViewController:vc animated:YES];
        
    } forControlEvents:UIControlEventTouchUpInside];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    if (@available(iOS 11.0, *)) {
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).offset(self.view.safeAreaInsets.top + 21.f);
        }];
    } else {
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).offset(85);
        }];
    }
}

#pragma mark - ISPasswordViewControllerDelegate
- (void) passwordDidChangedWithType:(ISPasswordViewControllerType)type {
    [[ISSafetyAlarmMamanger sharedInstance] startMonitoringWithStrategy:self.strategy];
    ISStopProtectViewController *vc = [ISStopProtectViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - private

NWLazyPropertyWithInit(UIImageView, desImgView, {
    _desImgView.contentMode = UIViewContentModeScaleAspectFit;
})

NWLazyPropertyWithInit(UILabel, titleLabel, {
    _titleLabel.font = [UIFont boldSystemFontOfSize:24.f];
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.numberOfLines = 1;
})

NWLazyPropertyWithInit(UILabel, desLabel, {
    _desLabel.font = [UIFont systemFontOfSize:18.f];
    _desLabel.textColor = [UIColor whiteColor];
    _desLabel.numberOfLines = 3;
})

NWLazyPropertyWithInit(UIImageView, bgImgView, {
    _bgImgView.contentMode = UIViewContentModeScaleToFill;
})

NWLazyPropertyWithInit(UIButton, actionButton, {
    _actionButton.backgroundColor = [UIColor clearColor];
    _actionButton.layer.cornerRadius = 29;
    _actionButton.clipsToBounds = YES;
    _actionButton.layer.borderWidth = 2.f;
    _actionButton.layer.borderColor = [UIColor nw_colorWithHex:0xffffff alpha:0.2].CGColor;
    [_actionButton setTitle:@"开启保护" forState:UIControlStateNormal];
    _actionButton.titleLabel.font = [UIFont systemFontOfSize:20.f];
    [_actionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
})

@end
